package com.example.volunteer.controller;


import com.example.volunteer.model.Skill;
import com.example.volunteer.service.SkillService;
import com.example.volunteer.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/skills")
public class SkillController {

    @Autowired
    private SkillService skillService;

    // 1. 创建新技能
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<String> createSkill(@RequestBody Skill skill) {
        skillService.insert(skill);
        return ResponseEntity.ok("技能创建成功");
    }

    // 2. 为志愿者添加技能
    @PostMapping("/volunteer/{volunteerId}")
    @PreAuthorize("hasAnyRole('ADMIN', 'VOLUNTEER')")
    public ResponseEntity<String> addSkillToVolunteer(
            @PathVariable String volunteerId,
            @RequestParam Integer skillId) {
        skillService.addVolunteerSkill(skillId, volunteerId);
        return ResponseEntity.ok("技能添加成功");
    }

    // 3. 移除志愿者的所有技能
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/volunteer/{volunteerId}")
    public ResponseEntity<String> removeAllSkillsFromVolunteer(
            @PathVariable String volunteerId) {
        skillService.removeAllVolunteerSkills(volunteerId);
        return ResponseEntity.ok("已移除该志愿者的所有技能");
    }

    // 4. 获取志愿者的所有技能
    @GetMapping("/volunteer")
    @PreAuthorize("hasAnyRole('ADMIN', 'VOLUNTEER')")
    public ResponseEntity<PageResult<Skill>> getSkillsByVolunteerId(
            @RequestParam String volunteerId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageResult<Skill> result = skillService.findByVolunteerId(volunteerId, pageNum, pageSize);
        return ResponseEntity.ok(result);
    }

    // 5. 获取所有可用技能（分页）
    @GetMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<PageResult<Skill>> getAllSkills(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageResult<Skill> result = skillService.findAll(pageNum, pageSize);
        return ResponseEntity.ok(result);
    }
}
